const _ = require('lodash')
const fs = require('fs')
const NP = require('number-precision')
const path = require('path')
const ROOT_DIR = path.join(__dirname, '..')
const HELPERS_DIR = path.join(ROOT_DIR, 'helpers')
const { readAllValues } = require(path.join(HELPERS_DIR, 'readAllValues.js'))
const allValues = readAllValues('m') // 读取数据
// 窗口长度
const windowWidth = 100

// 获取前32个数据作为第一批历史数据
const firstWindow = allValues.slice(0, windowWidth)
const remaining = allValues.slice(windowWidth) // 获取剩余部分

// 计算某个窗口内的热号和冷号
// const getHotAndColdNumbers = (windowData) => {
//   const numberCounts = _.countBy(windowData, 'number') // 统计号码出现次数
//   const topNumbers = getTopNumbers(numberCounts, 'desc', 16) // 热号（出现次数最多的16个）
//   const bottomNumbers = getTopNumbers(numberCounts, 'asc', 16) // 冷号（出现次数最少的16个）

//   return { topNumbers, bottomNumbers }
// }

const getHotAndColdNumbers = (windowData) => {
  const numberCounts = _.countBy(windowData, 'number') // 统计号码出现次数

  // 按出现次数排序
  const sortedNumbers = _.chain(numberCounts).toPairs().orderBy([1], ['desc']).value()

  // 获取前11个
  const topNumbers = sortedNumbers.slice(0, 11).map(([number]) => number)

  // 获取中间10个
  const middleStart = Math.floor((sortedNumbers.length - 10) / 2)
  const middleNumbers = sortedNumbers.slice(middleStart, middleStart + 10).map(([number]) => number)

  // 获取后11个
  const bottomNumbers = sortedNumbers.slice(-11).map(([number]) => number)

  return [...topNumbers, ...middleNumbers, ...bottomNumbers]
}

// 获取前n个数字，根据指定的排序方式（升序或降序）
const getTopNumbers = (numberCounts, order, take) => {
  return _.chain(numberCounts)
    .toPairs()
    .orderBy([1], [order]) // 按出现次数排序
    .take(take)
    .map(([number]) => number)
    .value()
}

// 保存热号和冷号的准确度
let results = []

// 初始化窗口（第一批数据）
let windowData = firstWindow

// 遍历剩余的号码数据
remaining.forEach((item, index) => {
  // 获取当前窗口的热号和冷号
  const numbers = getHotAndColdNumbers(windowData)

  // 计算当前号码是否为热号或冷号
  const accuracy = numbers.includes(item.number) ? 1 : 0

  // 保存结果
  results.push({
    ...item,
    accuracy
  })

  // 滑动窗口：移除窗口的第一项，加入当前项
  windowData = [...windowData.slice(1), item]

  // 每次滑动后，可以输出当前的热号和冷号（调试用）
  // console.log(`Current Window (${index + windowWidth}):`, { topNumbers, bottomNumbers })
})

// 输出结果
// fs.writeFileSync(path.join(__dirname, 'results.json'), JSON.stringify(results, null, 2), 'utf8')

const accuracy = results.map((item) => item.accuracy)
console.log(accuracy)

const accuracyCount = _.countBy(accuracy)
console.log(accuracyCount)

const successRate = NP.times(NP.round(NP.divide(accuracyCount[1], accuracy.length), 2), 100)

console.log(`${accuracyCount[1]} / ${accuracy.length} = ${successRate}%`)

console.log('--------------------------------')

// 计算准确统计
const { calcAccuracySummary, ACCURACY_SUMMARY_CN_MAP } = require(path.join(HELPERS_DIR, 'calcAccuracySummary.js'))

const accuracySummary = calcAccuracySummary(accuracy)
console.log(accuracySummary)

// 打印中文
Object.entries(ACCURACY_SUMMARY_CN_MAP).forEach(([key, value]) => {
  console.log(`${value}: ${accuracySummary[key]}`)
})
